var shareDAO = require('../models/shareDAO')
var formidable = require('formidable')
var path = require('path')
var shareController = {
        //分享圈
        shareContent: function (req, res) {
            shareDAO.shareContent(function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户分享失败' })
                } else {
                    res.json({ code: 200, data: results, msg: '用户分享成功!' })

                }
            })
        },

        //已发布内容
        publish: function (req, res) {
            // console.log("hahah:"+req.user)
            shareDAO.publish(req.user, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '获取用户发布内容失败' })
                }else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户发布内容成功!' })
                    } else {
                        res.json({ code: 200, msg: '获取用户发布内容失败' })
                    }
                }
            })
        },

        

        //分享圈详情
        sharedetail: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.sharedetail(upmenuId, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户获取分享圈详情失败' })
                } else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户获取分享圈详情成功!' })
                    } else {
                        res.json({ code: 200, msg: '用户获取分享圈详情失败' })
                    }
                }
            })
        },

        //已发布内容的详情
        publishdetail: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.publishdetail(upmenuId, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户获取发布内容详情失败' })
                } else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户获取发布内容详情成功!' })
                    } else {
                        res.json({ code: 200, msg: '用户获取发布内容详情失败' })
                    }
                }
            })
        },
         //上传菜谱 
         upmenu: function (req, res) {
            var upmenuTitle = req.body.upmenuTitle
            var finishPic = req.body.finishPic
            var story = req.body.story
            var touserId = req.body.touserId
            shareDAO.upmenu({ upmenuTitle: upmenuTitle, finishPic: finishPic, story: story, touserId: touserId }, function (err, results) {
                if (err) {
                    res.json({ code: 500, data: err.message, msg: '用户上传菜谱失败' })
                } else {
                       var stepId = req.body.stepId
                       var stepPic = req.body.stepPic
                       var stepMake = req.body.stepMake
                       var toupmenuId = req.body.toupmenuId
                    // res.json({ code: 200, data: results, msg: '用户上传菜谱成功!' })
                       shareDAO.upmenustep({stepId:stepId,stepPic:stepPic,stepMake:stepMake,toupmenuId:toupmenuId},function(err,result){
                           if(err){
                               res.json({code:500, msg:'用户上传菜谱失败'})
                           }else{
                            console.log('用户上传菜谱成功')
                            res.json({ code:200, data:results,msg:'用户上传菜谱成功'})
                            console.log(results)
                           }
                       })
                }
            })
        },

        //删除已发布菜谱
        personal: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.personalConment(upmenuId, function (err, results) {
                if (err) { 
                    res.json({ code: 500, msg: '删除已上传的菜谱失败' })
                } else {
                   shareDAO.deletepersonal(upmenuId, function (err, results){
                      if(err){
                          res.json({code:500, msg:'删除失败'})
                      }else{
                          console.log('成功删除上传的菜谱')
                          res.json({ code:200, data:results,msg:'删除成功'})
                          console.log(results)
                      }
                   })
                }
            })
        },   

 //增加分享圈点赞数
 addUserlike: function (req, res) {
    // var userId = req.body.userId
    shareDAO.selsharelike(req.user, function (err, results) {
        if (err) {
            res.json({ code: 500, msg: '查询点赞数失败' })
        } else {
            if (results.length >= 0) {
                var upmenuId = req.body.upmenuId
                var fs = 0;
                var flag = 1;
                if (results.length != 0) {
                    for (var i = 0; i < results.length; i++) {
                        if (upmenuId == results[i].toupmenuId) {
                            res.json({ code: 200, data: 0, msg: '该用户已经点过赞' })
                            flag = 0
                        }
                    }
                } else if (results.length == 0) {
                    fs = 1;
                    console.log(fs)
                }
                if (flag == 1 || fs == 1) {
                    // console.log(userId)
                    console.log(req.user.phone)
                    console.log(upmenuId)
                    shareDAO.insertLike({ upmenuId: upmenuId, user:req.user }, function (err, resinsert) {
                        if (err) {
                            res.json({ code: 500, msg: '在数据库中插入一条点赞记录失败！' })
                        } else {
                            if (resinsert.affectedRows == 1) {
                                shareDAO.addUserlike(upmenuId, function (err, resadd) {
                                    if (err) {
                                        res.json({ code: 500, msg: '数据库点赞增加错误' })
                                    } else {
                                        if (resadd.affectedRows == 1) {
                                            res.json({ code: 200, data: resadd.affectedRows, msg: '点赞成功' })
                                        } else {
                                            res.json({ code: 200, data: resadd.affectedRows, msg: '点赞失败' })
                                        }
                                    }
                                })
                            } else {
                                res.json({ code: 200, data: resinsert.affectedRows, msg: '插入一条点赞记录失败' })
                            }
                        }
                    })
                }
            } else {
                res.json({ code: 200, data: results, msg: '该用户没有点赞记录' })
            }
        }
    })
},
//取消分析圈点赞数
celUserlike: function (req, res) {
    // var userId = req.body.userId
    shareDAO.selsharelike(req.user, function (err, results) {
        if (err) {
            res.json({ code: 500, msg: '查询数据库失败' })
        } else {
            if (results.length != 0){
            var upmenuId = req.body.upmenuId
            var flag = 1
            for(var i = 0;i<results.length;i++){
                if(upmenuId == results[i].toupmenuId){
                    shareDAO.delLike({upmenuId:upmenuId,user:req.user},function(err,resdelete){
                        if (err) {
                              console.log(userId)
                              console.log(upmenuId)
                            res.json({ code: 500, msg: '在数据库中删除点赞记录失败！' })
                    }else{
                        if(resdelete.affectedRows == 1){
                          
                            shareDAO.celUserlike(upmenuId,function(err,resdel){
                                if(err){
                                    res.json({code:500,msg:'数据库点赞取消失败'})
                                }else{
                                    if (resdel.affectedRows == 1) {
                                        res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞成功！' })
                                    } else {
                                        res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞失败！' })
                                    }
                                }
                            })
                        }else {
                            res.json({ code: 200, data: resdelete.affectedRows, msg: '删除点赞记录失败！' })
                    }
                    
                }
            })
            flag=0
            }
        }
        if(flag==1){
            res.json({ code: 200, data: 0, msg: '该用没有点赞过这个套餐！' });
        }
    }else{
            res.json({ code: 200, data: results, msg: '取消点赞数成功' })
        }
    }

    })
}
    }
module.exports = shareController                                                                                                                                                                                                                                                   